package cn.minimelon.solon.listener;

import cn.hutool.core.collection.ListUtil;
import cn.hutool.core.util.StrUtil;
import cn.minimelon.solon.domain.ClearEvent;
import cn.minimelon.solon.mapper.clear.FusionReportMapper;
import lombok.extern.slf4j.Slf4j;
import org.beetl.sql.solon.annotation.Db;
import org.noear.solon.annotation.Component;
import org.noear.solon.core.event.EventListener;

import java.util.List;

@Slf4j
@Component
public class ClearEventListener implements EventListener<ClearEvent> {
    @Db("writer")
    private FusionReportMapper fusionReportMapper;

    @Override
    public void onEvent(ClearEvent clearEvent) throws Throwable {
        List<List<String>> grList = ListUtil.split(clearEvent.getIdList(), 500);
        for (List<String> gr : grList) {
            if (StrUtil.equals(clearEvent.getType(), "invoice")) {
                fusionReportMapper.deleteInvoiceDetail(gr);
            } else if (StrUtil.equals(clearEvent.getType(), "balance")) {
                int count = fusionReportMapper.deleteBalanceDetail(gr);
                log.info("delete balance count {}", count);
            }
        }
    }
}
